/**
 * 资源业务对象类
 *author:yhb
 */
package com.towker.frame.service.rbac.res;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;

import com.towker.core.db.*;
import com.towker.core.util.BeanUtils;
import com.towker.frame.dao.rbac.*;
import com.towker.frame.model.rbac.Resource;
import com.towker.frame.share.ServiceShare;

@Service
public class ResourceService extends BaseService<Resource, java.lang.Long>{
	private ResourceDao resourceDao;
	private RoleResourceDao roleResourceDao;
	public void setRoleResourceDao(RoleResourceDao roleResourceDao){this.roleResourceDao = roleResourceDao;}
	public void setResourceDao(ResourceDao resourceDao){this.resourceDao = resourceDao;}
	@SuppressWarnings("unchecked")
	@Override
	protected EntityDao getEntityDao(){return (EntityDao) this.resourceDao;}
	public void addResource(Resource res,String relatedRes){
		//添加父类基本资源
		add(res);
		//父类资源
		if(null==relatedRes||null==relatedRes.split(",")||0==relatedRes.split(",").length||"".equals(relatedRes))return;
		for(String tmp:relatedRes.split(",")){
			Resource child=new Resource();
			BeanUtils.copyProperties(child, res);
			child.setParentid(res.getId());
			child.setName(tmp.split("_")[0]);
			child.setUrl(tmp.split("_")[1]);
			child.setViewandoption(2L);
			add(child);
		}
	}
	/**根据系统删除所有资源*/
	public void delBySysid(long sysid){
	}
	/**根据资源编号以及该资源下的所有叶子资源删除资源*/
	public void delResAndResRelated(long id, String resids){
		String[] residArr = resids.split(",");
		List<Long> residList = new ArrayList<Long>();
		for (String resid : residArr){
			Long residLong = Long.parseLong(resid);
			residList.add(residLong);
		}
		this.roleResourceDao.delRoleResByResidList(residList);
		this.resourceDao.delById(id);
	}
	/**删除系统下的所有资源*/
	public void delResBySysid(long sysid){
		this.roleResourceDao.delBySysid(sysid);
	}
	/**删除系统资源*/
	public void delResource(Long id){
		this.resourceDao.delResource(id);
	}
	/**删除系统下的所有资源*/
	public void delResourceBySystemid(Long systemid){
		this.resourceDao.delResourceBySystemid(systemid);
	}
	/**给资源排序*/
	public void sortResource(String resIds){
		long i=0;
		for(String tmp:resIds.split(","))resourceDao.sortResource(new Long(tmp),new Long(++i));
	}
	/**修改资源*/
	public void updateResource(Resource res,String str){
		if(null==str||"".equals(str)){update(res);return;}
		else{res.setViewandoption(1L);update(res);}
		for(String tmp:str.split(",")){
			String[] arr=tmp.split("_");
			Resource child=new Resource();
			if(new Long(0L).equals(new Long(arr[2]))){
				BeanUtils.copyProperties(child, res);
				child.setParentid(res.getId());
				child.setName(arr[0]);
				child.setUrl(arr[1]);
				child.setViewandoption(2L);
				add(child);
				continue;
			}
			Resource po=getById(new Long(arr[2]));
			po.setName(arr[0]);
			po.setUrl(arr[1]);
			update(po);
		}
	}
	public List<Resource> getAllResBySysId(String sysid){
		long id = Long.parseLong(sysid);
		List<Resource> allRes = this.resourceDao.getAllResBySysId(id);
		return allRes;
	}
	/**根据角色编号，获得该角色的所有已经分配的资源。*/
	public List<Resource> getAssignedResOfRole(String roleid){
		long id = Long.parseLong(roleid);
		List<Resource> assignedRes = this.resourceDao.getAssignedResOfRole(id);
		return assignedRes;
	}
	/**获得父类资源下面的所有子资源*/
	public List<Resource> getChildrenResByIdAndSysid(long id, long sysid){
		return resourceDao.getChildrenResByIdAndSysid(id, sysid);
	}
	@SuppressWarnings("unchecked")
	public List getIconList(String fPath)
	{
		List list=new ArrayList();
		File f=new File(fPath);
		File[] arr=f.listFiles();
		for(File tmp:arr)list.add(tmp);
		return list;
	}
	/**
	 * 获得用户在当前系统的权限列表
	 * @param userid
	 * @param sysid
	 * @return
	 */
	public List<Resource> getPermissionListByUseridAndSysid(long userid, long sysid){
		return resourceDao.getAssignedResByUseridAndSysid(userid, sysid);
	}
	public List<Resource> getChildrenResById(long id){
		return resourceDao.getChildrenResById(id);
	}
	/**
	 * 根据角色编号获得该角色的所有分配资源
	 * @param roleid
	 * @return
	 */
	public List<Resource> getResByRoleid(long roleid){
		return resourceDao.getResByRoleid(roleid);
	}
	/**
	 * 根据系统编号获得该系统的所有资源
	 * @param sysid
	 * @return
	 */
	public List<Resource> getResBySysid(long sysid){
		return resourceDao.getResBySysid(sysid);
	}
	/**
	 * 获得角色的资源树
	 * @param sysid
	 * @param roleid
	 * @return
	 */
	public String getResourceTreeOfRole(long sysid,long roleid){
		List<Resource> sysRes=getResBySysid(sysid);
		List<Resource> roleRes=getResByRoleid(roleid);
		for(Resource sys:sysRes)
			for(Resource role:roleRes)
				if(sys.getId().equals(role.getId()))sys.setAssignedToRole(true);
		for(Resource sys:sysRes)sys.setOpen(true);
		return""+sysRes;
	}
	public String getResourceTreeBySystemid(long systemid){
		return""+resourceDao.getResBySysid(systemid);
	}
	public List<Resource> getRootResBySysid(long sysid){
		return resourceDao.getRootResBySysid(sysid);
	}
	/**
	 * 根据用户编号与系统编号获得用户资源列表
	 * @param userid
	 * @param sysid
	 * @return
	 */
	public List<Resource> getUserPermission(long userid,long sysid){
		return resourceDao.getUserPermission(userid,sysid);
	}
	/**
	 * 根据用户编号与系统编号获得用户资源列表
	 * @param userid
	 * @param sysid
	 * @return
	 */
	public List<Resource> getUserResListByUseridAndSysid(long userid, long sysid){
		return resourceDao.getAssignedResByUseridAndSysid(userid, sysid);
	}
	/**
	 * 获得用户资源树
	 * @param userid
	 * @param sysid
	 * @return
	 */
	public String getUserResJson(long userid,long sysid, int resType){
		return""+resourceDao.getUserRes(userid, sysid, resType);
	}
	public static void main(String[] args){
		ResourceService service = (ResourceService) ServiceShare.getService(ResourceService.class);
		String string = service.getResourceTreeOfRole(1, 1);
		System.out.println(string);
//		for(Resource tmp:service.getUserPermission(1L,1L)){
//			System.out.println(tmp);
//		}
	}
}
